package com.cy.ppj.service.user.impl;

import com.cy.ppj.comm.constant.CacheConstants;
import com.cy.ppj.dao.mapper.user.UserMapper;
import com.cy.ppj.dao.mapper.user.UserWechatMapper;
import com.cy.ppj.model.domian.user.User;
import com.cy.ppj.model.domian.user.UserWechat;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.user.UserWechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @description: 用户微信信息
 * @author: Ocean
 * @create: 2018-08-06 17:24
 **/
@Service
public class UserWechatServiceImpl extends BaseService implements UserWechatService {

    @Autowired
    private UserWechatMapper userWechatMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Cacheable(value = CacheConstants.CACHE_VALUE_2H, key = "'queryByOpenIdInCache-'+#openId")
    public UserWechat queryByOpenIdInCache(String openId) {
        UserWechat userWechat = new UserWechat();
        userWechat.setOpenId(openId);
        return userWechatMapper.selectOne(userWechat);
    }

    @Override
    @Cacheable(value = CacheConstants.CACHE_VALUE_2H, key = "'queryByUserIdInCache-'+#userId")
    public UserWechat queryByUserIdInCache(Long userId) {
        UserWechat userWechat = new UserWechat();
        userWechat.setUserId(userId);
        return userWechatMapper.selectOne(userWechat);
    }

    @Override
    @Transactional
    @Caching(evict = {
            @CacheEvict(value = CacheConstants.CACHE_VALUE_2H, key = "'queryByUserIdInCache-'+#userWechat.userId", allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.CACHE_VALUE_2H, key = "'queryByOpenIdInCache-'+#userWechat.openId", allEntries = true, beforeInvocation = true)
    })
    public void inserUserWechat(UserWechat userWechat) {

        //删除openId旧绑定关系
        UserWechat delOpenId = new UserWechat();
        delOpenId.setOpenId(userWechat.getOpenId());
        userWechatMapper.delete(delOpenId);

        //删除userId旧绑定关系
        UserWechat delUserId = new UserWechat();
        delUserId.setUserId(userWechat.getUserId());
        userWechatMapper.delete(delUserId);

        //新增
        Date time = new Date();
        userWechat.setCreateTime(time);
        userWechat.setUpdateTime(time);
        userWechatMapper.insertSelective(userWechat);

        //更新头像
        User user = new User();
        user.setId(userWechat.getUserId());
        user.setHeadImg(userWechat.getHeadImg());
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    @Transactional
    @Caching(evict = {
            @CacheEvict(value = CacheConstants.CACHE_VALUE_2H, key = "'queryByUserIdInCache-'+#userWechat.userId", allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.CACHE_VALUE_2H, key = "'queryByOpenIdInCache-'+#userWechat.openId", allEntries = true, beforeInvocation = true)
    })
    public void updateUserWechat(UserWechat userWechat) {
        userWechat.setUpdateTime(new Date());
        userWechatMapper.updateByPrimaryKeySelective(userWechat);

        //更新头像
        User user = new User();
        user.setId(userWechat.getUserId());
        user.setHeadImg(userWechat.getHeadImg());
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    @Transactional
    @Caching(evict = {
            @CacheEvict(value = CacheConstants.CACHE_VALUE_2H, key = "'queryByUserIdInCache-'+#userWechat.userId", allEntries = true, beforeInvocation = true),
            @CacheEvict(value = CacheConstants.CACHE_VALUE_2H, key = "'queryByOpenIdInCache-'+#userWechat.openId", allEntries = true, beforeInvocation = true)
    })
    public void deleteUserWechat(UserWechat userWechat) {
        userWechatMapper.delete(userWechat);
    }


}
